
package com.avrio.json;

// <editor-fold defaultstate="collapsed" desc="import">
import java.io.IOException;
import java.io.Reader;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
// </editor-fold>

@SuppressWarnings("serial")
public class JsonArray extends JsonValue implements Iterable<JsonValue> {

    private final List<JsonValue> values;

// <editor-fold defaultstate="collapsed" desc="JsonArray()">

    public JsonArray() {
        values = new ArrayList<JsonValue>();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="JsonArray()">

    public JsonArray(JsonArray array) {
        this( array, false );
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="JsonArray()">

    private JsonArray(JsonArray array, boolean unmodifiable) {

        if(array == null) {
            throw new NullPointerException("array is null");
        }
        if(unmodifiable) {
            values = Collections.unmodifiableList(array.values);
        }
        else {
            values = new ArrayList<JsonValue>(array.values);
        }
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="readFrom()">

    public static JsonArray readFrom(Reader reader) throws IOException {
        return JsonValue.readFrom(reader).asArray();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="readFrom()">

    public static JsonArray readFrom(String string) {
        return JsonValue.readFrom(string).asArray();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="unmodifiableArray()">

    public static JsonArray unmodifiableArray(JsonArray array) {
        return new JsonArray(array, true);
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="add()">

    public JsonArray add(int value) {
        values.add(valueOf(value));
        return this;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="add()">

    public JsonArray add(long value) {
        values.add(valueOf(value));
        return this;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="add()">

    public JsonArray add(float value) {
        values.add(valueOf(value));
        return this;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="add()">

    public JsonArray add(double value) {
        values.add(valueOf(value));
        return this;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="add()">

    public JsonArray add(boolean value) {
        values.add( valueOf(value));
        return this;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="add()">

    public JsonArray add(String value) {
        values.add(valueOf(value));
        return this;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="add()">

    public JsonArray add(JsonValue value) {

        if(value == null) {
            throw new NullPointerException("value is null");
        }
        values.add(value);
        return this;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="set()">

    public JsonArray set(int index, long value) {
        values.set(index, valueOf(value));
        return this;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="set()">

    public JsonArray set(int index, float value) {
        values.set(index, valueOf(value));
        return this;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="set()">

    public JsonArray set(int index, double value) {
        values.set(index, valueOf(value));
        return this;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="set()">

    public JsonArray set(int index, boolean value) {
        values.set(index, valueOf(value));
        return this;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="set()">

    public JsonArray set(int index, String value) {
        values.set(index, valueOf(value));
        return this;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="set()">

    public JsonArray set(int index, JsonValue value) {
        if(value == null) {
            throw new NullPointerException("value is null");
        }
        values.set(index, value);
        return this;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="size()">

    public int size() {
        return values.size();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="isEmpty()">

    public boolean isEmpty() {
        return values.isEmpty();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="get()">

    public JsonValue get(int index) {
        return values.get(index);
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="values()">

    public List<JsonValue> values() {
        return Collections.unmodifiableList(values);
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="iterator()">

    public Iterator<JsonValue> iterator() {

        final Iterator<JsonValue> iterator = values.iterator();
        return new Iterator<JsonValue>() {

            public boolean hasNext() {
                return iterator.hasNext();
            }

            public JsonValue next() {
                return iterator.next();
            }

            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="write()">

    @Override
    protected void write(JsonWriter writer) throws IOException {
        writer.writeArray(this);
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="isArray()">

    @Override
    public boolean isArray() {
        return true;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="asArray()">

    @Override
    public JsonArray asArray() {
        return this;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="hashCode()">

    @Override
    public int hashCode() {
        return values.hashCode();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="equals()">

    @Override
    public boolean equals(Object object) {

        if(this == object) {
            return true;
        }
        if(object == null) {
            return false;
        }
        if(getClass() != object.getClass()) {
            return false;
        }

        JsonArray other = (JsonArray)object;
        return values.equals(other.values);
    }
// </editor-fold>

}
